﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
*/

using System;
using MbUnit.Framework;
using WatiN.Core;
using DotNetNuke.Tests.UI.WatiN.Common;
using Gallio.Framework;
using DotNetNuke.Tests.UI.WatiN.Utilities;
using System.Configuration;
using WatiN.Core.DialogHandlers;
using DotNetNuke.Tests.UI.WatiN.Common.WatiNObjects;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;



namespace DotNetNuke.Tests.UI.WatiN.AdministratorTests
{
    [TestFixture]

    
    public class AdminSiteLogTests : ATBase
    {
        #region Test Setup and TearDown


        [SetUp]
        public void SetUp()
        {

            SetUpTest();
        }

        [TearDown]
        public void TearDown()
        {
            TearDownTest();
        }

        #endregion

        #region Tests

        [Test]
        public void Site_Log_Portal_Level()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);

            //Set the site log history to 0
            SiteSettingsPage settingsPage = new SiteSettingsPage(homePage);
            ieInstance.GoTo(SiteURL + "/Admin/SiteSettings.aspx");
            settingsPage.AdvancedSettingsTab.Click();
            settingsPage.HostSectionLink.Click();
            settingsPage.SiteLogHistoryField.Value = "0";
            settingsPage.UpdateLink.ClickNoWait();
            System.Threading.Thread.Sleep(1000);

            //Add A portal
            ieInstance.GoTo(SiteURL + "/Host/Portals.aspx");
            HostPortalPage portalPage = new HostPortalPage(homePage);
            portalPage.AddNewPortalLink.Click();
            portalPage.ChildRadioButton.Click();
            System.Threading.Thread.Sleep(3000);
            portalPage.PortalAliasField.Value = ("localhost/" + ApplicationName + "/MyTestPortal");
            portalPage.PortalTitle.Value = "My Test Portal";
            portalPage.PortalTemplateSelect.Select("Default Website");
            System.Threading.Thread.Sleep(3000);
            portalPage.AdminUserNameField.Value = TestUsers.Admin2.UserName;
            portalPage.AdminFirstNameField.Value = TestUsers.Admin2.FirstName;
            portalPage.AdminLastNameField.Value = TestUsers.Admin2.LastName;
            portalPage.AdminEmailField.Value = TestUsers.Admin2.Email;
            portalPage.AdminPasswordField.Value = TestUsers.Admin2.Password;
            portalPage.AdminConfirmField.Value = TestUsers.Admin2.Password;
            portalPage.CreatePortalLink.Click();
            portalPage.MessageLink.Click();

            //Act 
            ieInstance.GoTo(SiteURL + "/Admin/SiteLog.aspx");
            Assert.IsTrue(homePage.ModuleMessageSpan.InnerHtml.Contains("Your Hosting Provider Has Disabled the Site Log History"));
            ieInstance.GoTo(SiteURL + "/MyTestPortal/Admin/SiteLog.aspx");
            System.Threading.Thread.Sleep(1000);
            Assert.IsTrue(homePage.ModuleMessageSpan.InnerHtml.Contains("Your Hosting Provider Has Disabled the Site Log History"));

            ieInstance.GoTo(SiteURL + "/Admin/SiteSettings.aspx");
            settingsPage.AdvancedSettingsTab.Click();
            settingsPage.HostSectionLink.Click();
            settingsPage.SiteLogHistoryField.Value = "15";
            settingsPage.UpdateLink.ClickNoWait();
            System.Threading.Thread.Sleep(1000);

            ieInstance.GoTo(SiteURL + "/MyTestPortal/Admin/SiteSettings.aspx");
            settingsPage.AdvancedSettingsTab.Click();
            settingsPage.HostSectionLink.Click();
            settingsPage.SiteLogHistoryField.Value = "15";
            settingsPage.UpdateLink.ClickNoWait();
            System.Threading.Thread.Sleep(1000);

            ieInstance.GoTo(SiteURL + "/Admin/SiteLog.aspx");
            System.Threading.Thread.Sleep(1500);
            Assert.IsTrue(homePage.ModuleMessageSpan.InnerHtml.Contains("Limited Your Portal To 15 Days"));
            ieInstance.GoTo(SiteURL + "/MyTestPortal/Admin/SiteLog.aspx");
            System.Threading.Thread.Sleep(1000);
            Assert.IsTrue(homePage.ModuleMessageSpan.InnerHtml.Contains("Limited Your Portal To 15 Days"));

            //Clean up portal folders
            Directory.Delete(PhysicalPath + @"\MyTestPortal", true);
            Directory.Delete(PhysicalPath + @"\Portals\1", true);
        }

        [Test]
        public void Accessing_The_Logs()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);

            //Set the site log history to 0
            SiteSettingsPage settingsPage = new SiteSettingsPage(homePage);
            ieInstance.GoTo(SiteURL + "/Admin/SiteSettings.aspx");
            settingsPage.AdvancedSettingsTab.Click();
            settingsPage.HostSectionLink.Click();
            settingsPage.SiteLogHistoryField.Value = "15";
            settingsPage.UpdateLink.ClickNoWait();
            System.Threading.Thread.Sleep(1000);
            loginPage.LogoffUser();
            System.Threading.Thread.Sleep(1500);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);

            //Add a user for testing 
            ieInstance.GoTo(SiteURL + "/Admin/UserAccounts.aspx");
            UserPage userPage = new UserPage(homePage);
            userPage.AddUser(TestUsers.User1.UserName, TestUsers.User1.Password, TestUsers.User1.FirstName, TestUsers.User1.LastName, TestUsers.User1.EmailAddress, TestUsers.User1.DisplayName);
            System.Threading.Thread.Sleep(1000);

            //Act & Assert
            ieInstance.GoTo(SiteURL + "/Admin/SiteLog.aspx");
            AdminSiteLogPage siteLog = new AdminSiteLogPage(homePage);
            siteLog.ReportTypeSelectList.Select("Detailed Site Log");
            siteLog.DisplayReportLink.Click();
            Assert.IsTrue(siteLog.ReportTable.InnerHtml.Contains("SuperUser Account"));

            siteLog.ReportTypeSelectList.Select("Page Popularity");
            siteLog.DisplayReportLink.Click();
            Assert.IsTrue(siteLog.ReportTable.InnerHtml.Contains("Site Settings"));

            siteLog.ReportTypeSelectList.Select("Page Views By Day");
            siteLog.DisplayReportLink.Click();
            Assert.IsTrue(siteLog.ReportTable.InnerHtml.Contains("Views"));

            siteLog.ReportTypeSelectList.Select("Page Views By Day Of Week");
            siteLog.DisplayReportLink.Click();
            Assert.IsTrue(siteLog.ReportTable.InnerHtml.Contains("WeekDay"));

            siteLog.ReportTypeSelectList.Select("Page Views By Hour");
            siteLog.DisplayReportLink.Click();
            Assert.IsTrue(siteLog.ReportTable.InnerHtml.Contains("Hour"));

            siteLog.ReportTypeSelectList.Select("Page Views By Month");
            siteLog.DisplayReportLink.Click();
            Assert.IsTrue(siteLog.ReportTable.InnerHtml.Contains("Month"));

            siteLog.ReportTypeSelectList.Select("User Agents");
            siteLog.DisplayReportLink.Click();
            Assert.IsTrue(siteLog.ReportTable.InnerHtml.Contains("UserAgent"));

            siteLog.ReportTypeSelectList.Select("User Frequency");
            siteLog.DisplayReportLink.Click();
            Assert.IsTrue(siteLog.ReportTable.InnerHtml.Contains("SuperUser Account"));

            siteLog.ReportTypeSelectList.Select("User Registrations By Country");
            siteLog.DisplayReportLink.Click();
            Assert.IsTrue(siteLog.ReportTable.InnerHtml.Contains("Country"));

            siteLog.ReportTypeSelectList.Select("User Registrations By Date");
            siteLog.DisplayReportLink.Click();
            Assert.IsTrue(siteLog.ReportTable.InnerHtml.Contains("Date Registered"));

        }


        
        #endregion
    }
}